﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;

namespace Bot.Domain.Categories.Service
{
    public class AnswerInChannelLocationRequestDomainService : IAnswerInChannelLocationRequestDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, AnswerInChannelLocationRequest> _repository;

        public AnswerInChannelLocationRequestDomainService(IRepository<Guid, AnswerInChannelLocationRequest> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public AnswerInChannelLocationRequest Create(AnswerInChannelLocationRequest answerInChannelLocationRequest)
        {
            if(answerInChannelLocationRequest.Id == Guid.Empty)
            {
                answerInChannelLocationRequest.Id = Guid.NewGuid();
            }
            return _repository.Create(answerInChannelLocationRequest);
        }

        public AnswerInChannelLocationRequest Get(Guid id)
        {
            return _repository.Get(id);
        }

        public AnswerInChannelLocationRequest Update(AnswerInChannelLocationRequestUpdateBo bo)
        {
            AnswerInChannelLocationRequest answerInChannelLocationRequest = _repository.Get(bo.Id);
            answerInChannelLocationRequest.Id = bo.Id;
            answerInChannelLocationRequest.Message = bo.Message;
            answerInChannelLocationRequest.ButtonText = bo.ButtonText;

            return answerInChannelLocationRequest;

        }

        public IEnumerable<AnswerInChannelLocationRequest> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            AnswerInChannelLocationRequest answerInChannelLocationRequest = _repository.Get(id);
            if (answerInChannelLocationRequest != null)
            {
                IIntentAnswerInChannelDomainService intentAnswerInChannelDomainService = _container.Resolve<IIntentAnswerInChannelDomainService>();
                var intentAnswerInChannel = intentAnswerInChannelDomainService.GetByLocationRequestId(id);
                if (intentAnswerInChannel != null)
                {
                    intentAnswerInChannelDomainService.UpdateForLocationRequestId(intentAnswerInChannel.Id, Guid.Empty);
                }
                _repository.Delete(answerInChannelLocationRequest);
            }
        }

        public AnswerInChannelLocationRequest GetByAnswerInChannelId(Guid answerInChannelId)
        {
            IIntentAnswerInChannelDomainService intentAnswerInChannelDomainService = _container.Resolve<IIntentAnswerInChannelDomainService>();
            var intentAnswerInChannel = intentAnswerInChannelDomainService.Get(answerInChannelId, "locationRequest");

            return intentAnswerInChannel.LocationRequest;
        }
        public AnswerInChannelLocationRequest CreateWithAnswerInChannelId(Guid answerInChannelId, AnswerInChannelLocationRequest answerInChannelLocationRequest)
        {
            answerInChannelLocationRequest = Create(answerInChannelLocationRequest);
            IIntentAnswerInChannelDomainService intentAnswerInChannelDomainService = _container.Resolve<IIntentAnswerInChannelDomainService>();
            intentAnswerInChannelDomainService.UpdateForLocationRequestId(answerInChannelId, answerInChannelLocationRequest.Id);

            return answerInChannelLocationRequest;
        }
    }
}
